
--[[
	登录流程

--]]

local skynet = require("skynet")
local cjson = require("cjson")
---------------------------------------------------------
-- class
---------------------------------------------------------
local Handler = class("Handler")

function Handler:ctor(name)

	self.client_net = new("client_net")
	self.timer = new("timer")
	self:register()
end


function Handler:register()
	g.dispatch:register("games.RES_PING", handler(self, self.resPing))
	g.dispatch:register("games.RES_LOGIN", handler(self, self.resLogin))
	g.dispatch:register("games.RES_REGISTER", handler(self, self.resRegister))


	self:callback("games.RES_DESK_MATCH", self.resDeskMatch)
	self:callback("games.RES_DESK_ENTER", self.resDeskEnter)
	self:callback("games.RES_DESK_LEAVE", self.resDeskLeave)
	self:callback("games.RES_DESK_SITDOWN", self.resDeskSitdown)
	self:callback("games.RES_DESK_SITUP", self.resDeskSitup)
end

function Handler:onStart()

	--连接gate
	local ip = "127.0.0.1"
	local port = 60022
	local res = self.client_net:connect(ip, port)
	if not res then
		return
	end


	--定时器
	self.timer:start({sec=3, callback=handler(self, self.reqRegister)} )
	--self.timer:start({sec=3, callback=handler(self, self.reqLogin)} )
	--self.timer:start({sec=5, callback=handler(self, self.pingReq)} )
end

function Handler:test(...)
	print("test______", ...)

end

function Handler:reqPing()
	-- body
	local data = {
		time = os.time(),
	}
	self.client_net:send("games.REQ_PING", data)
end

function Handler:resPing(data)

	print("pingRes:", tostring(data))
end

--登录请求
function Handler:reqLogin( ... )
	local file_path = "/tmp/client_data.json"
	local record = load_json_file(file_path)
	local account_type = "gw_token"
	if not record.aid or not record.token or record.token == "" then
		account_type = "gw_account"
		record.extra = {
			name = "test",
			password = "123456",
		}
	end
	local data = {
		account_type = account_type,
		aid = record.aid,
		openid = record.openid or "",
		token = record.token or "",
	}
	if record.extra then
		data.extra = cjson.encode(record.extra)
	end
	print("record:",data)
	self.client_net:send("games.REQ_LOGIN", data)
end

--登录返回
function Handler:resLogin(data)
	print("resLogin data:", data)
	local file_path = "/tmp/client_data.json"
	local record = {}
	if data.error_code == 0 then
		print("111")
		--登录成功
		record.token = data.token
		record.aid = data.aid
		--保存下token
		save_file(file_path, cjson.encode(record))

		self:reqPing()
		self:reqDeskMatch()
		return
	end
	if data.error_code ~= 0 then
		print("222")
		--token 过期
		record = load_json_file(file_path)
		record.token = ""
		g.log:info("resLogin record:",record)
		save_file(file_path, cjson.encode(record))
		return
	end
	return
end

function Handler:reqRegister()
	local data = {
		phone = "13652307316",
		password = "123456",
	}
	self.client_net:send("games.REQ_REGISTER", data)
end

--登录返回
function Handler:resRegister(msg)
	g.log:info("resRegister data:", msg)
	if msg.error_code == 0 then
		print("注册成功！")
	elseif msg.error_code == 10002 then
		--帐号存在，直接登录
		local data = {
			account_type = "gw_account",
			extra = {
				name = "13652307316",
				password = "123456",
			}
		}
		if data.extra then
			data.extra = cjson.encode(data.extra)
		end
		self.client_net:send("games.REQ_LOGIN", data)
	end
end

function Handler:reqDeskMatch()
	local data = {
	}
	self.client_net:send("games.REQ_DESK_MATCH", data)
end
function Handler:resDeskMatch(msg)
	g.log:info("resDeskMatch data:", msg)
	self:reqDeskEnter(msg.table_id)
end

function Handler:reqDeskEnter(table_id)
	local data = {
		table_id = table_id,
	}
	self.client_net:send("games.REQ_DESK_ENTER", data)
end
function Handler:resDeskEnter(msg)
	g.log:info("resDeskEnter data:", msg)
	g.timeout(2, function ()
		self:reqDeskSitdown(msg.table_id)
	end)
end



function Handler:reqDeskLeave(table_id)
	local data = {
		table_id = table_id,
	}
	self.client_net:send("games.REQ_DESK_LEAVE", data)
end
function Handler:resDeskLeave(msg)
	g.log:info("resDeskLeave data:", msg)
end

function Handler:reqDeskSitdown(table_id)
	local data = {
		table_id = table_id,
	}
	self.client_net:send("games.REQ_DESK_SITDOWN", data)
end
function Handler:resDeskSitdown(msg)
	g.log:info("resDeskSitdown data:", msg)
end

function Handler:reqDeskSitup(table_id)
	local data = {
		table_id = table_id,
	}
	self.client_net:send("games.REQ_DESK_SITUP", data)
end
function Handler:resDeskSitup(msg)
	g.log:info("resDeskSitup data:", msg)
end

return Handler